﻿using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
using Jinndev.UI;
using System.IO;
using System.Text;
using System.Threading;

namespace Jinndev {

    public class TcbHostingUI : BaseUIBehaviour {

        #region static
        public static readonly string PATH = "Prefab/UI/TcbHosting/TcbHostingUI";

        public static TcbHostingUI Create() {
            return UIManager.Instance.Add<TcbHostingUI>(PATH, UIParam.FullScreen);
        }
        #endregion



        public RectTransform inputProjectArea;
        public Dropdown dropdownLocalWebDir;
        public Button btnStart;
        public RectTransform logArea;

        private InputDropdown inputProject;
        private LogView logView;

        private List<string> localWebDirs = new List<string>();
        private List<string> validWebDirs = new List<string> { "web-mobile" }; // 可用的资源目录

        private void Awake() {
            Setting.Instance.PrepareProjectPath();

            // 工程路径
            inputProject = InputDropdown.Create(inputProjectArea, new InputDropdown.Setting { placeholder = "支持拖拽" });
            inputProject.ResetOptions(Setting.Instance.projectPathList);
            inputProject.onValueChanged.AddListener(OnProjectChanged);

            logView = LogView.Create(logArea);

            DragAndDropHelper.Add(gameObject, OnDroppedFiles);
        }

        private void OnDestroy() {
            Setting.Instance.projectPath = inputProject.text;
            Setting.Save();
        }

        private void Start() {
            inputProject.text = Setting.Instance.projectPath;
        }


        private void Update() {
            TryInvokeAllActions();
        }

        private void OnDroppedFiles(List<string> aPathNames) {
            foreach (string path in aPathNames) {
                if (Directory.Exists(path)) {
                    inputProject.text = path;
                    return;
                }
            }
            logView.LogError("文件选择错误");
        }

        private void OnProjectChanged(string value) {
            localWebDirs.Clear();
            dropdownLocalWebDir.ClearOptions();

            string buildDir = Path.Combine(inputProject.text, "build");
            if (!Directory.Exists(buildDir)) {
                return;
            }

            string[] buildDirs = Directory.GetDirectories(buildDir);
            foreach (string dir in buildDirs) {
                string name = Path.GetFileName(dir);

                if (validWebDirs.Contains(name)) {
                    localWebDirs.Add(name);
                }
            }

            dropdownLocalWebDir.AddOptions(localWebDirs);
            Refresh();
        }

        private void Refresh() {
            bool hasProject = !string.IsNullOrEmpty(inputProject.text) && Directory.Exists(inputProject.text);
            bool hasLocalWebDir = dropdownLocalWebDir.value < dropdownLocalWebDir.options.Count;
            btnStart.interactable = hasProject && hasLocalWebDir;
        }

        public void OnClickStart() {
            btnStart.interactable = false;

            var startInfo = CommonUtil.CreateProcessStartInfo("test.bat", "", Encoding.GetEncoding("utf-8"), true, true);
            //startInfo.UseShellExecute = true;
            //startInfo.CreateNoWindow = false;

            var thread = new Thread(() => {
                CommonUtil.StartProcess(startInfo, text => {
                    Debug.Log(text);
                }, error => {
                    Debug.LogError(error);
                }, true, out _);
                Debug.Log("Done");
            });
            thread.IsBackground = false;
            thread.Start();

            btnStart.interactable = true;
        }


    }

}
